﻿using System;
using System.Collections;
using System.Collections.Generic;
using TheDarkKnight;
using UnityEngine;
using static TheDarkKnight.TempletObjectBase;

public class SpwanModle : MonoBehaviour
{

    public TempletObjectBase ModelTemplet;

    public MeshFilter m_Filter;

    // Start is called before the first frame update
    void Start()
    {
        SpwanModleByTemplet();
    }

    private void SpwanModleByTemplet()
    {
        List<Vector3> verticesList = new List<Vector3>();
        List<Vector2> UVList = new List<Vector2>();
        List<Vector3> normalList = new List<Vector3>();
        List<int> triangleList = new List<int>();
        for (int i = 0; i < ModelTemplet.Vectices.Count;i++)
        {
            VerticesData data = ModelTemplet.Vectices[i];
            verticesList.Add(data.Vertices);
            UVList.Add(data.UV);
            normalList.Add(data.Normal);
        }
        for (int j = 0; j < ModelTemplet.Triangles.Count;j++) {
            TriangelData data = ModelTemplet.Triangles[j];
            triangleList.Add(data.FirstVertice);
            triangleList.Add(data.SecondVertice);
            triangleList.Add(data.ThridVertice);
        }
        Mesh templetMesh = new Mesh();
        templetMesh.vertices = verticesList.ToArray();
        templetMesh.uv = UVList.ToArray();
        templetMesh.normals = normalList.ToArray();
        templetMesh.triangles = triangleList.ToArray();
        m_Filter = this.gameObject.AddComponent<MeshFilter>();
        m_Filter.mesh = templetMesh;
        MeshRenderer render = this.gameObject.AddComponent<MeshRenderer>();
        render.material = new Material(Shader.Find("Standard"));
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
